package com.coderside.core.util;

import static java.util.Locale.ENGLISH;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ParseUtil {

	/**
	 * 判断是否包含有html标签
	 * @param html
	 * @return
	 */
	public static boolean hasTag(String tag, String html) {
		String regEx = "<" + tag + ">"; //表示标签   
		Pattern p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(html);
		return m.find();
	}

	/**
	 * 去掉所有html标签
	 * @param html
	 * @return
	 */
	public static String replaceTag(String html) {
		String regEx = "<.+?>"; //表示标签   
		Pattern p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(html);
		//		System.out.println(m.find());
		String text = m.replaceAll("");
		text = text.trim();
		text = text.replace("　　", "");
		//		System.out.println(text.replace("　　", ""));
		return text;
	}

	public static boolean matcherReg(String regEx, String content) {
		Pattern p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(content);
		return m.find();
	}

	public static int matcherStartIndex(String regEx, String content) {
		Pattern p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(content);
		m.find();
		return m.start();
	}

	/**
	 * 根据正则表达式 拆分字符串、包含匹配字符串
	 * @return
	 */
	public static String[] splitString(String regEx, String content) {
		List<String> list = new ArrayList<String>();
		Pattern p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(content);
		int index = 0;
		while (m.find()) {
			String match = "";
			if (index != 0) {
				match = content.subSequence(index - 2, m.start()).toString();
				list.add(match);
			} else {
				match = content.subSequence(index, m.start()).toString();
			}
			index = m.end();
		}
		String endcontent = content.subSequence(index - 2, content.length()).toString();
		list.add(endcontent);
		String[] res = new String[list.size()];
		return list.toArray(res);
	}

	/**
	 * 去掉字符串中的TAB、回车；\t：TAB；\r\n:回车；
	 * @param str 输入的字符串
	 * @return
	 */
	public static String replaceSpecialCharacter_t_r_n(String str) {
		Pattern pt = Pattern.compile("\t|\r|\n");
		Matcher mt = pt.matcher(str);
		return mt.replaceAll("");
	}

	/**
	 * 提取：
	 * 执行结果为name.txt，提取的字符串储存在m.group(i)中，其中i最大值为m.groupCount();
	 * @throws Exception
	 */
	public static String getRegex(String regEx, String content, int index) {
		Pattern pattern = Pattern.compile(regEx);
		Matcher matcher = pattern.matcher(content);
		matcher.find();
		if (index <= matcher.groupCount()) {
			return matcher.group(index);
		}
		return "";
	}

	/**
	 * Returns a String which capitalizes the first letter of the string.
	 */
	public static String capitalize(String name) {
		if (name == null || name.length() == 0) {
			return name;
		}
		return name.substring(0, 1).toUpperCase(ENGLISH) + name.substring(1);
	}
}
